package com.dongdongshop.aop.aop;

import cn.hutool.json.JSONUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect //添加切面类注解
@Component //添加注入spring容器的注解 @Controller .@Service .@Repository .@Component 替代配置文件Bean
public class LogAop {

    //注入日志打印类
    private Logger logger = LoggerFactory.getLogger(LogAop.class);

    @Pointcut("execution(* com.dongdongshop.service.*.*(..)) || @annotation(com.dongdongshop.log.DkLog)")
    public void doPointcut(){

    }

    //前置通知
    @Before("doPointcut()")
    public void doBefore(JoinPoint joinPoint) throws Throwable{
        Object[] args = joinPoint.getArgs();//被调用的参数
        Signature signature = joinPoint.getSignature();//被调用方法的名字
        logger.info("方法 {} 正在被调用 , 参数为: {} ",signature,args);
    }

    //返回后通知
    @AfterReturning(value = "doPointcut()",returning = "o")
    public void doAfterReturning(Object o) throws Throwable{
        logger.info("方法调用成功,返回值为: {} ", JSONUtil.toJsonStr(o));
    }
}
